<?php

namespace common\models\business;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "business_contract_category".
 *
 * @property string $id
 * @property string $title
 * @property integer $parentid
 * @property integer $tatus
 */
class BusinessContractCategory extends \yii\db\ActiveRecord
{


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '合同分类',      //用于修改了什么的显示
        'mainfield' => 'title',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];

    const STATUS_ACTIVE = 1;
    const STATUS_DEAD = 0;

    public static $statuses = [
        self::STATUS_ACTIVE => '生效',
        self::STATUS_DEAD => '失效'
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_contract_category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'status'], 'required'],
            [['status'], 'integer'],
            [['title'], 'string', 'max' => 255],
            [['showpre'], 'safe'],
        ];
    }



    public function beforeSave($insert)
    {
        if(is_array($this->showpre)){
            $this->showpre =implode(',',$this->showpre);
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '分类名称',
            'parentid' => '上级',
            'status' => '状态',
            'showpre' => '需要展示的字段'
        ];
    }


    public static function allCate()
    {
        return BusinessContractCategory::find()->asArray()->all();
    }


    /**
     * @param $model
     * @return array
     * 获取合同显示的字段
     */
    public static function showPre($model)
    {
        if(!empty($model->showpre)){
            return $model->showpre = explode(',',$model->showpre);
        }
        return [];
    }

}
